package com.twenty.seven.apollo.demo.kk;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        Bootstrap b = new Bootstrap();
        b.group(workerGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new ClientHandler());
                    }
                });
        ChannelFuture cf = b.connect("localhost",8765).sync();
        cf.channel().writeAndFlush(Unpooled.copiedBuffer("777".getBytes()));
        cf.channel().closeFuture().sync();
        workerGroup.shutdownGracefully();
    }
    public String replaceSpace(StringBuffer str) {
        String result = null;
        for(int i = 0; i<str.length(); i++){
            String g = str.substring(i,i+1);
            if(" ".equals(g)){
                result += "%20";
            }else{
                result += g;
            }
        }
        return result;
    }
}
